﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using log4net.Repository.Hierarchy;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class UserAddressController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? id)
        {
            if (id < 1 || id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);

            }
            var model = ConsumerBLL.Instance.GetModel(id.Value);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
            ViewBag.Id = model.Id;
            ViewBag.UserName = model.NickName;
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length, string whereStr, int id)
        {
            var model = ConsumerBLL.Instance.GetModel(id);
            if (model == null)
            {
                return Json(false);
            }
            try
            {
                int order;
                int.TryParse(Request["order[0][column]"], out order);
                string direction = Request["order[0][dir]"];
                var orderByColumns = new[] { "LinkMan", "LinkMobile", "Address", "AddressDetail", "IsDefault" };
                string search = Request["search[value]"];
                TableDataViewDto<ConsumerAddress> theList = ConsumerAddressBLL.Instance.GetPagingList(draw, (start / length) + 1, length, SqlSafe(search),
                    orderByColumns[order] + " " + direction, id);
                JsonResult jsonResult = Json(theList);
                jsonResult.MaxJsonLength = int.MaxValue;
                return jsonResult;
            }
            catch (Exception ex)
            {
                Logger.Error("UserAddressController  Listing Catch Error: " + ex.Message);
                return Json(false);
            }
        }
        public ActionResult SetDefaultAddress(int id)
        {
            var model = ConsumerAddressBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Conflict);
            }
            ConsumerAddressBLL.Instance.DeleteDefaultAddress(model.ConsumerId);
            model.IsDefault = true;
            if (ConsumerAddressBLL.Instance.Update(model))
            {
                return RedirectToAction("List", "UserAddress", new { id = model.ConsumerId });
            }
            return new HttpStatusCodeResult(HttpStatusCode.Conflict);
        }
        public ActionResult Create(int id)
        {
            if (id < 1)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Conflict);
            }
            var model = ConsumerBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Conflict);
            }
            ViewBag.ConsumerId = model.Id;
            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection collection, ConsumerAddress model)
        {
            var result = new Result() { Status = false, Message = "添加失败" };
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            var consumerAddress = ConsumerAddressBLL.Instance.GetModel(model.ConsumerId);
            model.IsDefault = model.IsDefault;
            model.AreaId = 1;
            if (model.IsDefault)
            {
                ConsumerAddressBLL.Instance.DeleteDefaultAddress(model.ConsumerId);
            }
            int resultId = ConsumerAddressBLL.Instance.Add(model);
            if (resultId > 0)
            {
                result.Status = true;
                result.Message = resultId.ToString();
            }
            return Json(result);
        }

        public ActionResult Update(int id)
        {
            if (id < 1)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Conflict);
            }
            var model = ConsumerAddressBLL.Instance.GetModel(id);
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Conflict);
            }
            ViewBag.ConsumerId = model.ConsumerId;
            return View(model);
        }
        [HttpPost]
        public ActionResult Update(FormCollection collection, ConsumerAddress model)
        {
            var result = new Result() { Status = false, Message = "修改失败" };
            var consumerAddress = ConsumerAddressBLL.Instance.GetModel(model.Id);
            if (consumerAddress == null)
            {
                return Json(result);
            }
            consumerAddress.Latitude = model.Latitude;
            consumerAddress.LinkMan = model.LinkMan;
            consumerAddress.LinkMobile = model.LinkMobile;
            consumerAddress.Longitude = model.Longitude;
            consumerAddress.AddressDetail = model.AddressDetail;
            consumerAddress.IsDefault = model.IsDefault;
            if (!string.IsNullOrEmpty(model.Address))
            {
                consumerAddress.Address = model.Address.Trim();
            }
            if (model.IsDefault)
            {
                ConsumerAddressBLL.Instance.DeleteDefaultAddress(consumerAddress.ConsumerId);
            }
            result.Status = ConsumerAddressBLL.Instance.Update(consumerAddress);
            if (result.Status)
            {
                result.Status = true;
                result.Message = consumerAddress.Id.ToString();
            }
            return Json(result);
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public ActionResult GetActiveShopList(string searchKeywords, int id)
        {
            var consumerAddress = ConsumerAddressBLL.Instance.GetModel(id);
            if (consumerAddress == null)
            {
                return Json("");
            }
            int searchShopRadius = 2000;
            var sysConfig = SysConfigBLL.Instance.GetModelList().FirstOrDefault();
            if (sysConfig == null)
            {
                return Json("");
            }
            searchShopRadius = sysConfig.SearchShopRadius > 0 ? sysConfig.SearchShopRadius : 2000;
            var lsShops = ShopsBLL.Instance.GetMatchedShopList(consumerAddress.Longitude, consumerAddress.Latitude, searchShopRadius, searchKeywords);
            if (lsShops.Count == 0)
            {
                return Json("");
            }
            return Json(lsShops);
        }
    }
}